﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.ComponentModel;

namespace ErrorProviders
{
    internal class ErrorProviderHelper
    {
        internal void ValidateUIElement(IInternalErrorProvider errorProvider, UIElement element)
        {
            ValidateDataCallback method = errorProvider.InternalGetValidateMethod(element);

            if (method == null)
            {
                method = errorProvider.ValidateMethod;
            }

            string errorMessage = string.Empty;
            InternalObjectProperties objectProp = null;
            if (method != null && !method(element))
            {
                errorMessage = errorProvider.InternalGetErrorMessage(element);

                if (string.IsNullOrEmpty(errorMessage))
                    errorMessage = errorProvider.ErrorMessage;

                objectProp = new InternalObjectProperties();
                if (!string.IsNullOrEmpty(errorMessage))
                {
                    if (element is FrameworkElement)
                    {
                        objectProp.ToolTip = ((FrameworkElement)element).ToolTip;
                        ((FrameworkElement)element).ToolTip = errorMessage;
                    }
                }

                object val = errorProvider.InternalGetErrorValue(element);

                if (val == null)
                    val = errorProvider.ErrorValue;

                string errorPropertyModified = (string)element.GetValue(errorProvider.InternalErrorPropertyModifiedProperty);

                if (string.IsNullOrEmpty(errorPropertyModified))
                    errorPropertyModified = errorProvider.ErrorPropertyModified;

                //Sauvegarde de l'ancienne valeur. Permet la remise à 0 lors de modification
                objectProp.Val = element.GetType().GetProperty(errorPropertyModified).GetValue(element, null);
                if (!errorProvider.OldValues.ContainsKey(element))
                    errorProvider.OldValues.Add(element, objectProp);

                this.ChangePropertyValue(errorProvider, element, val);

            }
            else
            {
                if (errorProvider.OldValues.ContainsKey(element))
                    this.ChangePropertyValue(errorProvider, element, errorProvider.OldValues[element]);
            }

            if (element is Panel)
            {
                Panel panel = element as Panel;
                foreach (UIElement child in panel.Children)
                    ValidateUIElement(errorProvider, child);
            }
        }

        internal void RestoreDefaultValue(IInternalErrorProvider errorProvider, UIElement element)
        {
            if (errorProvider.OldValues.ContainsKey(element))
            {
                this.ChangePropertyValue(errorProvider, element, errorProvider.OldValues[element]);
                errorProvider.OldValues.Remove(element);
            }

            if (element is Panel)
            {
                foreach (UIElement child in ((Panel)element).Children)
                {
                    RestoreDefaultValue(errorProvider, child);
                }
            }
        }

        private void ChangePropertyValue(IInternalErrorProvider errorProvider, UIElement element, object val)
        {
            string prop = errorProvider.InternalGetErrorPropertyModified(element);

            if (string.IsNullOrEmpty(prop))
                prop = errorProvider.ErrorPropertyModified;

            if (!string.IsNullOrEmpty(prop) && val != null)
            {
                if (val is String)
                    ConvertFromString(element, prop, val);
                else
                    element.GetType().GetProperty(prop).SetValue(element, val, null);

            }
        }

        private void ChangePropertyValue(IInternalErrorProvider errorProvider, UIElement element, InternalObjectProperties val)
        {
            this.ChangePropertyValue(errorProvider, element, val.Val);
            if (element is FrameworkElement)
            {
                ((FrameworkElement)element).ToolTip = val.ToolTip;
            }
        }

        private void ConvertFromString(UIElement element, string prop, object val)
        {

            TypeConverter typeConverter = TypeDescriptor.GetConverter(element.GetType().GetProperty(prop).PropertyType);
            if (typeConverter != null)
            {
                element.GetType().GetProperty(prop).SetValue(element, typeConverter.ConvertFromString((string)val), null);
            }

        }


    }
}
